package com.forester.foresterplatform.system.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
@ApiModel(description = "用户实体类")
@TableName("system_user")
@EqualsAndHashCode(callSuper = true)
public class User extends BaseEntity implements UserDetails {

    /**
     * 主键
     */
    @ApiModelProperty("主键")
    private Long id;

    /**
     * 用户名
     */
    @ApiModelProperty("用户名")
    private String username;

    /**
     * 性别 0-男 1-女
     */
    @ApiModelProperty("性别 0-男 1-女")
    private Integer gender;
    /**
     * 密码
     */
    @ApiModelProperty("密码")
    private String password;

    /**
     * 身份证号
     */
    @ApiModelProperty("身份证号")
    private String idCard;

    /**
     * 邮箱
     */
    @ApiModelProperty("邮箱")
    private String email;

    /**
     * 电话
     */
    @ApiModelProperty("电话")
    private String phone;

    /**
     * 生日
     */
    @ApiModelProperty("生日")
    private LocalDateTime birthday;

    /**
     * 年龄
     */
    @ApiModelProperty("年龄")
    private Integer age;

    /**
     * 状态 1-正常 2-禁用
     */
    @ApiModelProperty("状态 1-正常 2-禁用")
    private Integer status;

    /**
     * 上次登录时间
     */
    @ApiModelProperty("上次登录时间")
    private LocalDateTime lastLogin;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", gender=" + gender +
                ", password='" + password + '\'' +
                ", idCard='" + idCard + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", birthday=" + birthday +
                ", status=" + status +
                ", lastLogin=" + lastLogin +
                "} " + super.toString();
    }

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    @TableField(exist = false)
    private List<GrantedAuthority> grantedAuthorities;

    /**
     * 获取当前用户有那些权限
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(Objects.isNull(grantedAuthorities) && permissionsStrings != null && permissionsStrings.size() != 0) {  // 避免每次调用重新生成
            return grantedAuthorities = permissionsStrings.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    /**
     * 账号是否过期
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账号是否锁定
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 账号密码是否过期
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     *
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }

    /**
     * 一个用户对应多个角色
     */
    @ApiModelProperty("一个用户对应多个角色")
    @TableField(exist = false)
    private List<Role> roles;

    @ApiModelProperty(hidden = true)
    @TableField(exist = false)
    private List<String> permissionsStrings;
}
